import React from "react";

class Clock extends React.Component{
    constructor(props){
        super(props)
        this.state={date:new Date()};
    }
    //渲染函数
    render(){
        return (
            <div>
                <h1>这是一个时间组件,用来显示当前的时间</h1>
                <h2>当前时间是:{this.state.date.toLocaleTimeString()}</h2>
            </div>
        )
    }
    componentWillUnmount(){
        clearInterval(this.timerID)
    }

    //生命周期
    componentDidMount(){
        this.timerID=setInterval(()=>this.tick(),1000);
    }

    //时间更新
    tick(){
        this.setState({
            date:new Date()
        })
    }
}

export default Clock